/*******************************************************************************
 * Copyright 2013 Mindaugas Idzelis
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package incontrol.bf3.state;

import incontrol.bf3.protocol.events.datatype.PlayerInfo;
import incontrol.bf3.protocol.events.datatype.Squad;

import java.util.Date;

import com.google.common.base.Objects;

public class PlayerState {
	public final String name;
	public final String GUID;
	public final int teamId; // 0=nuetral, 1-16 only
	public final int squadId; // 0=nuetral, 1-16 only
	public final int kills;
	public final int deaths;
	public final int score;
	public final long joinTime; // or time client was made aware of players
								// existence

	public PlayerState(PlayerInfo playerInfo) {
		this(playerInfo, System.currentTimeMillis());
	}

	private PlayerState(PlayerInfo playerInfo, long joinTime) {
		this.name = playerInfo.name;
		this.GUID = playerInfo.guid;
		this.teamId = playerInfo.teamId;
		this.squadId = playerInfo.squadId;
		this.kills = playerInfo.kills;
		this.deaths = playerInfo.deaths;
		this.score = playerInfo.score;
		this.joinTime = joinTime;
	}

	public PlayerState(String name, String GUID, int teamId, int squadId,
			int kills, int deaths, int score, long joinTime) {
		super();
		this.name = name;
		this.GUID = GUID;
		this.teamId = teamId;
		this.squadId = squadId;
		this.kills = kills;
		this.deaths = deaths;
		this.score = score;
		this.joinTime = joinTime;
	}

	public PlayerState setTeam(int team) {
		return new PlayerState(name, GUID, team, squadId, kills, deaths, score,
				joinTime);
	}

	public PlayerState setSquad(int team, int squad) {
		return new PlayerState(name, GUID, team, squad, kills, deaths, score,
				joinTime);
	}

	public PlayerState setSquad(Squad squad) {
		return setSquad(squad.teamId, squad.squadId);
	}

	public PlayerState update(PlayerInfo playerInfo) {
		return new PlayerState(playerInfo, joinTime);
	}

	@Override
	public String toString() {
		return Objects.toStringHelper(getClass().getSimpleName())
				.add("name", name).add("guid", GUID).add("teamId", teamId)
				.add("squadId", squadId).add("kills", kills)
				.add("deaths", deaths).add("score", score)
				.add("jointime", new Date(joinTime)).toString();
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(name, GUID, teamId, squadId, kills, deaths,
				score, joinTime);
	}

	@Override
	public boolean equals(Object object) {
		if (object instanceof PlayerState) {
			PlayerState that = (PlayerState) object;
			return Objects.equal(this.name, that.name)
					&& Objects.equal(this.GUID, that.GUID)
					&& Objects.equal(this.teamId, that.teamId)
					&& Objects.equal(this.squadId, that.squadId)
					&& Objects.equal(this.kills, that.kills)
					&& Objects.equal(this.deaths, that.deaths)
					&& Objects.equal(this.score, that.score)
					&& Objects.equal(this.joinTime, that.joinTime);
		}
		return false;
	}

}
